package com.suxii.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import androidx.annotation.Nullable;

/*
圆形可打开控件
 */
public class CircleOpenView extends View {

    private Paint paint;
    private Path path;

    //控件的宽高
    private float w,h;

    private boolean isOpen=false;

    private ValueAnimator animator;

    public CircleOpenView(Context context) {
        this(context,null);
    }

    public CircleOpenView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CircleOpenView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint=new Paint();

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        w=getWidth();
        h=getHeight();
        r=h/4;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawOpen(canvas);
    }


    //中间圆的半径
    private float r=150;
    //外圈圆的半径
    private float r2=0;

    //圆弧起始点
    private float arc=135;
    private void drawOpen(Canvas canvas){


        int center_x=getWidth()/2;
        int center_y=getHeight()/2;

        //阴影
        setLayerType(LAYER_TYPE_SOFTWARE,paint);
        BlurMaskFilter blu=new BlurMaskFilter (7,BlurMaskFilter.Blur.SOLID);
        paint.setMaskFilter(blu);
        paint.setShadowLayer(20,5,5,0x000000);

        //Log.e("aa",getWidth()+":"+getMeasuredWidth());
        //画外圈圆形
        paint.setColor(0xff9d91ff);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(8);
        canvas.drawCircle(center_x,center_y,r2,paint);

        //画内圈圆形的外环边框
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        paint.setColor(0xff7463f9);
        canvas.drawCircle(center_x,center_y,r+2,paint);

        //画扇形选择区域
        paint.setStyle(Paint.Style.FILL);
        if (changeValue==1){
            paint.setColor(0xaadb5860);
        }else {
            paint.setColor(0xff624fff);
        }
        //当我们设置为true的时候，绘制的时候就经过圆心了
        canvas.drawArc(center_x-r2,center_y-r2,center_x+r2,center_y+r2,arc,90,true,paint);


        //画内圈圆形
        paint.setColor(0xff7463f9);
        canvas.drawCircle(center_x,center_y,r,paint);

        if (isMsg) {
            paint.setColor(0xaadb5860);
            canvas.drawArc(center_x - r, center_y - r, center_x + r, center_y + r, 45, 90, true, paint);
        }
        paint.setColor(0xff624fff);
        canvas.drawCircle(center_x,center_y,r/2,paint);

    }

    private void openAnim(){
        if (isOpen) {
            animator = ValueAnimator.ofFloat(0, r2);
            //Log.e("aa","on");
        }else {
            animator=ValueAnimator.ofFloat(r2,0);
            //Log.e("aa","off");
        }
        animator.setDuration(300);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.addUpdateListener(v -> {
            //Log.e("aa",v.getAnimatedValue()+"值");
            r2= (float) v.getAnimatedValue();
            invalidate();
        });
    }


    public interface ChangeListener{
        void onChangeListener(int value);

        void onChanged(int value);

        void onTouchDown(boolean isTouchDown);
    }

    private ChangeListener changeListener;

    public void setOnChangeListener(ChangeListener changeListener){
        this.changeListener=changeListener;
    }

    private boolean isMsg=false;
    public void receiveMsg(boolean isMsg){
        this.isMsg=isMsg;
        invalidate();
    }


    private float onX,onY;
    //触摸半径
    private float touchR=r;
    //选择的值
    private int changeValue;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                onX=event.getX();
                onY=event.getY();
                if (onX<w/2+touchR&&onX>w/2-touchR && onY<h/2+touchR&&onY>h/2-touchR) {
                    isOpen = true;
                    r2=h/2;
                    openAnim();
                    animator.start();
                    //Log.e("aa","isc");
                    changeListener.onTouchDown(true);
                }else
                    return false;
                break;
            case MotionEvent.ACTION_MOVE:
                float x=event.getX()-onX;
                float y=event.getY()-onY;
                if (y<-r){//上
                    arc=-135;
                    changeValue=0;
                }else if (y>r){//下
                    arc=45;
                    changeValue=2;
                }else {//中
                    arc=135;
                    changeValue=1;
                }
                changeListener.onChanged(changeValue);
                //Log.e("aa",x+"x");

                break;
            case MotionEvent.ACTION_UP:
                if (isOpen) {
                    isOpen = false;
                    //arc = 135;
                    openAnim();
                    animator.start();
                    changeListener.onChangeListener(changeValue);
                    changeListener.onTouchDown(false);
                }
                break;
        }
        invalidate();

        return true;
    }

}
